exports.__esModule = true;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
  constructor() {
    super();
    this.cusFormModel = this.db['oa_cust_form'];

    this.typeGroupModel = this.db['sys_type_group'];
    this.typeModel = this.db['sys_type'];

    this.typeGroupModel.hasMany(this.typeModel, { foreignKey: 'typegroupid' });
    // this.cusFormModel.belongsTo(this.typeModel,{
    //   foreignKey:'typecode',
    //   targetKey:'asdasd'
    // })
    // this.typeModel.hasMany(this.cusFormModel, {
    //   foreignKey: "template_group",
    //   constraints: false,
    //   targetKey: "typecode"
    // });
  }
  /**
   * 保存/更新自定义表单
   *
   * @param {any} data
   * @returns
   */
  saveCustForm(data) {
    var _this = this;

    return _asyncToGenerator(function* () {
      console.log(data);
      try {
        const Op = _this.db.Sequelize.Op;
        const oldUser = yield _this.cusFormModel.findOne({
          where: {
            template_code: data.template_code
          },
          attributes: ['id']
        });
        if (data.id) {
          if (oldUser && oldUser.id != data.id) {
            return false;
          } else {
            return yield _this.cusFormModel.update(data, {
              where: {
                id: data.id
              }
            });
          }
        } else {
          console.log(3333);
          if (oldUser) {
            return false;
          } else {
            return yield _this.cusFormModel.upsert(data);
          }
        }
      } catch (error) {
        console.log(error);
        return _this.exceptionService.handleError(error);
      }
    })();
  }
  /**
   * 获取表单图标信息
   *
   */
  getCusFormIcon() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      try {
        const url = './www/static/upload/customTemplate/';
        const imgPath = '/static/upload/customTemplate/';
        const a = _fs2.default.readdirSync(url);
        const res = [];
        let obj = {};
        for (let i = 0; i < a.length; i++) {
          obj.img = imgPath + a[i];
          obj.showing = false;
          res.push(obj);
          obj = {};
        }
        return res;
      } catch (error) {
        return _this2.exceptionService.handleError(error);
      }
    })();
  }
  /**
   * 获取所有表单模板信息
   *
   * @returns
   */
  getCusFormInfo(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const where = {
        form_type: data.form_type
      };
      if (data.template_name) {
        where.template_name = {
          [_this3.db.Sequelize.Op.like]: '%' + data.template_name + '%'
        };
      }

      try {
        return yield _this3.cusFormModel.findAll({
          where: where
        });
      } catch (error) {
        return _this3.exceptionService.handleError(error);
      }
    })();
  }

  /**
   * 通过id查询表单模板信息
   *
   * @param {any} data
   */
  getCusFormInfoById(data) {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      try {
        const serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');
        const Res = yield _this4.cusFormModel.findOne({
          where: {
            id: data.id
          },
          attributes: ['id', 'template_name', 'template_code', 'form_items', 'create_by', 'update_by', 'create_date', 'update_date', 'icon_path']
        });
        if (Res) {
          Res.icon_path = serverIP + Res.icon_path;
        }
        // let arr = [];
        // arr = Res.map(item => {
        //   item.icon_path = serverIP + item.icon_path;
        //   return item;
        // });
        return Res;
      } catch (error) {
        return _this4.exceptionService.handleError(error);
      }
    })();
  }

  /**
   * 通过id删除模板
   *
   * @param {any} data
   * @returns
   */
  delCusFormById(data) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      try {
        const Res = yield _this5.cusFormModel.destroy({
          where: {
            id: data.id
          }
        });
        return Res;
      } catch (error) {
        return _this5.exceptionService.handleError(error);
      }
    })();
  }

  // 移动端接口

  /**
   * 获取所有信息
   *
   * @returns
   */
  getCusFormSimpleInfoByAll() {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      try {
        const cusForm = yield _this6.cusFormModel.findAll({
          where: {
            form_type: 1
          },
          attributes: ['id', 'template_name', 'template_code', 'icon_path', 'template_group']
          // group: "template_group"
        });
        const typeGroup = yield _this6.typeGroupModel.findOne({
          where: {
            typegroupcode: 'bdfz'
          },
          include: [{
            model: _this6.typeModel,
            attributes: ['typecode', 'typename']
          }]
        });
        const small = typeGroup.sys_types;
        const serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');
        const data = small.map(function (item) {
          item.dataValues.template = item.dataValues.template || [];
          cusForm.forEach(function (item2) {
            const data = {
              id: item2.id,
              template_name: item2.template_name,
              template_code: item2.template_code,
              template_group: item2.template_group,
              template_icon: serverIP + item2.icon_path
            };
            if (item.typecode == item2.template_group) {
              item.dataValues.template.push(data);
            }
          });

          return item;
        });
        return data;
      } catch (error) {
        return _this6.exceptionService.handleError(error);
      }
    })();
  }
};